package main.链表;

import common.ListNode;

/**
 * @author tmh
 * @date 2024/7/19 15:49
 * @description
 */
public class T21合并两个有序链表 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode list3 = new ListNode(0);
        ListNode p = list3;
        //当list1和list2不为空的时候进行我们的判断操作
        while (list1 != null && list2 != null) {
            int num1 = list1.val;
            int num2 = list2.val;
            if (num1 >= num2) {
                list3.next = list2;
                list2 = list2.next;
            } else {
                list3.next = list1;
                list1 = list1.next;
            }
            list3 = list3.next;
        }
        //这里用三元表达式判断更简单
        list3.next = list1 == null ? list2 : list1;
        return p.next;
    }
}
